/*
 * IntelliJ IDEA: .
 * Copyright 2014 Ifoly.inc. All right reserved.
 *
 * path:
 * author: liuhui
 * version: 1.0.0
 */
define(function (require, exports, module) {
    var action_hbs = require('hbs!./common/action_one');
    var TABLE = function() {
        // 初始化
        var dt = new Datatable();

        function initDT() {
            dt.init({
                src: $("#profit_dt"),
                onSuccess: function(grid) {},
                onError: function(grid) {
                },
                loadingMessage: '加载中...',
                dataTable: {
                    columns: [{
                        data: 'adate'
                    },{
                        data: 'sum_principal'
                    }, {
                        data: 'actual'
                    }, {
                        data: 'sum_profit'
                    }, {
                        data: 'sum_adjust_amount'
                    }, {
                        data: null
                    }],
                    columnDefs: [
                        {
                            targets: 0,
                            render: function (data, type, full, meta) {
                                return data.split(" ",1)
                            }
                        },
                        DTKit.renderActionColumn(function(data, type, full, row) {
                            return action_hbs(
                                {
                                    name:'查看明细',
                                    url: g.ctx + 'report/profit/detail_index/?date='+ full['adate']
                                });
                        })
                    ],
                    "order": [
                        [0, "desc"]
                    ]
                }
            });
        }

        function bindEvents() {
            // 查询
            $('#search_btn').click(function(e) {
                dt.setAjaxParam("multiquery", "1");
                var s_date = $('#s_date').val();
                dt.setAjaxParam("s_date", s_date);
                dt.getDataTable().ajax.reload();

                var url = g.ctx + "report/profit/count_json";
                $.post(url,{
                    s_date :s_date
                }).done(function(obj){
                    $("#t_sum_principal").text(obj.t_sum_principal==null?0:obj.t_sum_principal);
                    $("#t_actual").text(obj.t_actual==null?0:obj.t_actual);
                    $("#t_sum_profit").text(obj.t_sum_profit==null?0:obj.t_sum_profit);
                    $("#t_sum_adjust_amount").text(obj.t_sum_adjust_amount==null?0:obj.t_sum_adjust_amount);
                });
            });

            $('#index_export').click(function(e) {
                var url = g.ctx + "report/profit/index_export";
                var closest = $(this).closest("form");
                closest.attr("action",url);
                closest.submit();
            });


            $('#s_date').daterangepicker({
                opens: (MbaJF.isRTL() ? 'left' : 'right'),
                format: 'YYYY/MM/DD'
            });
        }

        return {
            init: function() {
                initDT();
                bindEvents();
            }
        };
    }();
    return {
        init: function() {
            TABLE.init();
        }
    };
});